package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.BatchConfirmReceiveReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BatchConfirmReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;

/**
 * 批量确认收货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchConfirmReceive {
	private static BatchConfirmReceiveReq batchConfirmReceiveReq;
	private static BatchConfirmReceiveCall batchConfirmReceiveCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量确认收货");
	}

	@Before
	public void caseUp() {

		batchConfirmReceiveReq = new BatchConfirmReceiveReq();
		batchConfirmReceiveCall = new BatchConfirmReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchConfirmReceiveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchConfirmReceive_001() {
		Logger.start(true, "批量确认收货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			List<Long> orders = new ArrayList<Long>();

			for (int i = 0; i < 2; i++) {

				TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
						.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

				long orderId = placeOrderResp.getOrderList().get(0)
						.getOrderId();

				long sellerId = placeOrderResp.getOrderList().get(0)
						.getSellerId();

				long userId = placeOrderResp.getUserId();

				long tradingId = placeOrderResp.getTradingId();

				BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
				//订单支付
				TradingCallService
						.payOrder(userId, useAccountAmount, tradingId);
				//接单
				TradingCallService.sellerAcceptOrder(sellerId, orderId);
				//发货
				TradingCallService.delivery(sellerId, orderId,
						LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

				orders.add(orderId);
			}

			Logger.comment("前置数据准备结束");
			// 准备参数
			batchConfirmReceiveReq.setOrderIds(orders);

			batchConfirmReceiveReq.setRemark("收到了啊");

			batchConfirmReceiveCall.setData(batchConfirmReceiveReq);
			batchConfirmReceiveCall.callService();

			Logger.verifyEquals(200,
					batchConfirmReceiveCall.getCode(), "验证code");
			// 数据库验证
			for (long orderId : batchConfirmReceiveReq.getOrderIds()) {

				Ts_ConfirmReceive.verifyOrderStatus(orderId,
						batchConfirmReceiveReq.getRemark(), 0L);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchConfirmReceive_002() {
		Logger.start(true, "批量确认收货,其中有订单状态为不能发货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			long userId = placeOrderResp.getUserId();

			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
			//订单支付
			TradingCallService.payOrder(userId, useAccountAmount, tradingId);
			//接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			//发货
			TradingCallService.delivery(sellerId, orderId, false);

			TradingServiceResp placeOrderResp2 = TradingCallService
					.orderAccept();

			long orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			// 准备参数 
			batchConfirmReceiveReq
					.setOrderIds(Arrays.asList(orderId, orderId2));

			batchConfirmReceiveReq.setRemark("收到了啊");

			batchConfirmReceiveCall.setData(batchConfirmReceiveReq);
			batchConfirmReceiveCall.callService();
			Logger.verifyEquals(200,
					batchConfirmReceiveCall.getCode(), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
